package itsm.isperp.module.service.problem;

import itsm.isperp.framework.algorithm.Similarity;
import itsm.isperp.module.entity.problem.ProbAutoTriggerRule;
import itsm.isperp.module.service.problem.rule.Counter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 问题自动发起规则解析器
 * 
 * @author lizx
 * @date 2015-7-26
 * @since 1.0
 * 
 */
public abstract class ProbAutoTriggerRulePaser {

	// 组合
	public static Map<String, Counter> paser4(List<Map<String, Object>> result,
			ProbAutoTriggerRule rule) {

		Map<String, Counter> rmapper = new HashMap<String, Counter>();
		Map<String, Counter> mapper = paser3(result, rule);

		for (String key : mapper.keySet()) {
			List<Map<String, Object>> vs = mapper.get(key).getMapList();
			Map<String, Counter> r = paser2(vs, rule);
			rmapper.putAll(r);
		}
		return rmapper;

	}

	public static Map<String, Counter> paser3(List<Map<String, Object>> result,
			ProbAutoTriggerRule rule) {

		Map<String, Counter> mapper = new HashMap<String, Counter>();

		Map<String, Counter> rmapper = new HashMap<String, Counter>();
		String id = null;
		String key = null;

		for (Map<String, Object> m : result) {
			id = m.get("id").toString();
			key = m.get("akey").toString();
			Counter cou = null;
			if (rmapper.containsKey(key)) {

				cou = rmapper.get(key);
				cou.increase();
				cou.addToList(id);
				cou.addToMapList(m);

			} else if (mapper.containsKey(key)) {

				cou = mapper.get(key);
				cou.increase();
				cou.addToList(id);
				cou.addToMapList(m);
				if (cou.getCount() >= rule.getOccurTimes()) {
					mapper.remove(key);
					rmapper.put(key, cou);

				}

			} else {

				cou = new Counter();
				cou.addToList(id);
				mapper.put(key, cou);
				cou.addToMapList(m);
				if (cou.getCount() >= rule.getOccurTimes()) {
					mapper.remove(key);
					rmapper.put(key, cou);
				}
			}
		}
		mapper.clear();
		for (String k : rmapper.keySet()) {
			Counter c = rmapper.get(k);
			mapper.putAll(paser2(c.getMapList(), rule));

		}

		return mapper;
	}

	// 只做标题匹配
	public static Map<String, Counter> paser2(List<Map<String, Object>> result,
			ProbAutoTriggerRule rule) {

		Map<String, Counter> mapper = new HashMap<String, Counter>();

		Map<String, String> rs = new HashMap<>();
		for (Map<String, Object> m1 : result) {
			rs.put(m1.get("id").toString(), m1.get("title").toString());
		}

		Float f = rule.getSimilarValue() / 100;

		doParser2(rs, new ArrayList<String>(), mapper, rule.getOccurTimes(),
				f.doubleValue());

		return mapper;

	}

	private static void doParser2(Map<String, String> rs,
			List<String> toRemoves, Map<String, Counter> mapper,
			int occurTimes, double sim) {

		for (String str : toRemoves) {
			rs.remove(str);
		}

		String t1;
		String t2;
		String id2 = null;
		for (String k : rs.keySet()) {
			t1 = rs.get(k);
			Counter cou = new Counter();
			cou.addToList(k);

			for (String k2 : rs.keySet()) {

				id2 = k2;
				t2 = rs.get(k2);

				double d = Similarity.similarDegree(t1, t2);
				if (d > sim) {
					cou.increase();
					cou.addToList(id2);
				}
			}
			if (cou.getCount() > occurTimes) {
				mapper.put(k, cou);
				doParser2(rs, cou.getList(), mapper, occurTimes, sim);
				return;
			}

		}
	}

	public static Map<String, Counter> paser(List<Map<String, Object>> result,
			ProbAutoTriggerRule rule) {
		Map<String, Counter> mapper = new HashMap<String, Counter>();

		Map<String, Counter> rmapper = new HashMap<String, Counter>();
		String id = null;
		String key = null;

		for (Map<String, Object> m : result) {
			id = m.get("id").toString();
			key = m.get("akey").toString();

			if (rmapper.containsKey(key)) {

				Counter cou = rmapper.get(key);
				cou.increase();
				cou.addToList(id);

			} else if (mapper.containsKey(key)) {

				Counter cou = mapper.get(key);
				cou.increase();
				cou.addToList(id);
				if (cou.getCount() >= rule.getOccurTimes()) {
					mapper.remove(key);
					rmapper.put(key, cou);
				}

			} else {

				Counter cou = new Counter();
				cou.addToList(id);
				mapper.put(key, cou);
				if (cou.getCount() >= rule.getOccurTimes()) {
					mapper.remove(key);
					rmapper.put(key, cou);
				}
			}
		}

		return rmapper;
	}

}
